package com.ordering.action.admin;


import java.security.NoSuchAlgorithmException;

import com.ordering.action.BaseAction;
import com.ordering.entity.Manager;
import com.ordering.entity.UserInfo;
import com.ordering.service.login.AdminLoginService;
import com.ordering.service.login.impl.AdminLoginServiceImpl;
import com.ordering.util.MD5;

public class AdminLoginAction extends BaseAction
{
    //输入
    private String loginName;
    private String password;
    private String imageCode;
    //输出
    private Manager user;
    private String message;
    
    
    AdminLoginService service = new AdminLoginServiceImpl();
    
    public String execute() {
        try
        {
            Manager po = new Manager();
            po.setLoginName(loginName);
            po.setPassword(MD5.crypt(password.trim()));
            user = service.login(po);
            if(user == null){
                message = "用户名或密码错误，请重新登录";
                return FAIL;
            }else {
                //封装UserInfo对象并放入session中
                UserInfo userInfo = new UserInfo();
                userInfo.setId(user.getId());
                userInfo.setPassword(user.getPassword());
                userInfo.setUsername(user.getName());
                userInfo.setUserRole(UserInfo.USER_ROLE_ADMIN);
                session.put("user", userInfo);
                return SUCCESS;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public String getLoginName()
    {
        return loginName;
    }

    public void setLoginName(String loginName)
    {
        this.loginName = loginName;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getImageCode()
    {
        return imageCode;
    }

    public void setImageCode(String imageCode)
    {
        this.imageCode = imageCode;
    }

    public Manager getUser()
    {
        return user;
    }

    public void setUser(Manager user)
    {
        this.user = user;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }
}
